/**
 * Title:UserDepartmentMapper<br>
 * Description:用户部门DAL接口<br>
 * Copyright © Xiamen Dripstill  NetWorks Co.,Ltd.
 * @Create:2018-07-03<br>
 * @author:Sun Yijing<br>
 * @version: Object v1.0.0<br>
 * <br>
 * 修改历史:<br>
 * 修改人		修改日期		修改描述<br>
 * -----------------------------------<br>
 * <br>
 * <br>
 */
package com.yoooya.base.dao.system;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.yoooya.base.dao.DalMapper;
import com.yoooya.base.model.po.system.UserDepartmentPo;
import com.yoooya.base.model.qo.system.UserDepartmentQo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@DalMapper
public interface UserDepartmentMapper {
	/**
	 * 根据条件查询记录集(不分页)
	 *
	 * @param qo
	 *            条件查询对象
	 * @return 返回查询列表
	 * @throws Exception
	 */
	List<UserDepartmentPo> query(@Param("qo") UserDepartmentQo qo)
			throws Exception;

	/**
	 * 根据条件查询记录集(采用Paginator组件进行分页)
	 *
	 * @param qo
	 *            条件查询对象
	 * @param pageBounds
	 *            组件分页对象
	 * @return 返回查询列表
	 * @throws Exception
	 */
	List<UserDepartmentPo> query(@Param("qo") UserDepartmentQo qo,
                                 PageBounds pageBounds) throws Exception;

	/**
	 * 根据主键查询记录
	 *
	 * @param id
	 *            主键ID
	 * @return 返回查询对象
	 * @throws Exception
	 */
	UserDepartmentPo queryById(Long id) throws Exception;

	/**
	 * 根据条件查询记录总数
	 *
	 * @param qo
	 *            条件查询对象
	 * @return 返回统计记录
	 * @throws Exception
	 */
	int count(@Param("qo") UserDepartmentQo qo) throws Exception;

	/**
	 * 根据主键删除记录
	 *
	 * @param id
	 * @return 返回删除结果
	 * @throws Exception
	 */
	int deleteById(Long id) throws Exception;

	/**
	 * 根据ID列表批量删除记录
	 *
	 * @param ids
	 *            ID列表
	 * @return 返回删除结果
	 * @throws Exception
	 */
	@Delete("delete from UserDepartment where id in (${ids})")
	int deleteByBatch(@Param("ids") String ids) throws Exception;

	/**
	 * 保存记录,不管记录里面的属性是否为空
	 *
	 * @param record
	 *            插入记录
	 * @return 返回插入结果
	 * @throws Exception
	 */
	int insert(UserDepartmentPo record) throws Exception;

	/**
	 * 保存记录,不管记录里面的属性是否为空
	 *
	 * @param record
	 *            插入记录
	 * @return 返回插入结果
	 * @throws RuntimeException
	 */
	int insertByBatch(@Param("records") List<UserDepartmentPo> records)
			throws RuntimeException;

	/**
	 * 根据主键更新属性不为空的记录
	 *
	 * @param record
	 *            更新记录对象
	 * @return 返回更新结果
	 * @throws Exception
	 */
	int updateById(UserDepartmentPo record) throws Exception;

	/**
	 * 根据主键批量更新属性不为空的记录
	 *
	 * @param ids
	 *            更新ID列表
	 * @param record
	 *            更新记录对象
	 * @return 返回更新结果
	 * @throws Exception
	 */
	@Update("update UserDepartment set createTime=#{record.createTime} where id in (${ids})")
	int updateByBatch(@Param("ids") String ids,
                      @Param("record") UserDepartmentPo record) throws Exception;

	/**
	 * 批量修改状态
	 *
	 * @param ids
	 *            更新ID列表
	 * @param status
	 *            批量修改状态
	 * @return 返回更新结果
	 * @throws Exception
	 */
	@Update("update UserDepartment set status=#{status} where id in (${ids})")
	int updateStatusByBatch(@Param("ids") String ids,
                            @Param("status") Integer status) throws Exception;

	@Delete("delete from UserDepartment where userId=#{userId}")
	int deleteByUser(@Param("userId") Long userId) throws Exception;
	
	@Select("select group_concat(departmentId) from UserDepartment where userId=#{userId}")
	String queryByUser(@Param("userId") Long userId) throws Exception;

	String querySameDeptUsersByUserId(@Param("userId") Long userId) throws Exception;

    String queryUserIdsByDeptIds(@Param("qo") UserDepartmentQo qo);
}